﻿package igz.fleaxy.fx {
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import igz.fleaxy.util.ImageUtil;
	import igz.fleaxy.util.ObjectUtil;
	import com.greensock.TweenMax;

	/**
	 * @author Intelygenz | www.intelygenz.com
	 * 
	 * Genera una distorsión en un elemento
	 */
	public class FxMotionDistort {

		static public function $Reset(p_displayObject:DisplayObject) : void {
			new FxMotionDistort()._Reset(p_displayObject);
		}

		/**
		 * Introduce una distorsión a un elemento seleccionado
		 * 
		 * @param	p_displayObject Objeto a distorsionar
		 * @param	p_settings { "duration" : 0.5
										 , "distortX" : 30
										 , "distortY" : 1
										 , "glowColor" : 0xFFFFFF //color de distorsion
										 , "autoReset" : false  // se auto-reinicia
										 , "onComplete" : null  //función a ejecutar cuando termine
										 };
		 */
		static public function $Start(p_displayObject:DisplayObject, p_settings:Object = null) : void {
			new FxMotionDistort()._Start(p_displayObject, p_settings);
		}

		protected var _Settings:Object = { "duration" : 0.5
										 , "distortX" : 30
										 , "distortY" : 1
										 , "glowColor" : 0xFFFFFF
										 , "autoReset" : false
										 , "onComplete" : null
										 };

		public function FxMotionDistort() {

		}

		protected function _Start(p_displayObject:DisplayObject, p_settings:Object = null) : void {
			ObjectUtil.$Merge(p_settings, _Settings);

			TweenMax.killTweensOf(p_displayObject);
			TweenMax.to(p_displayObject
							, 0.2
							, { blurFilter : { blurX : _Settings["distortX"], blurY : _Settings["distortY"] }
							  , yoyo : true
							  }
						);
			TweenMax.to(p_displayObject
							, 1
							, { glowFilter : { color: _Settings["glowColor"] , alpha:0.8, strength : 2, blurX : 20, blurY : 20 }
							  , onComplete : _OnComplete
							  , onCompleteParams : [p_displayObject]
							  }
						);
		}

		protected function _Reset(p_displayObject:DisplayObject) : void {
			TweenMax.killTweensOf(p_displayObject);
			TweenMax.to(p_displayObject, 0.2
									, { blurFilter : { blurX : 1, blurY : 1, alpha : 0 }
									  , glowFilter : { blurX : 1, blurY : 1, alpha : 0 }
									  }
						);
		}

		protected function _OnComplete(p_displayObject:DisplayObject) : void {
			if ( _Settings["autoReset"] ) {
				_Reset(p_displayObject);
			}
			if ( _Settings["onComplete"] != null ) {
				_Settings["onComplete"]();
			}
		}

	}

}